//
//  StartViewController.m
//  Share_Home_F
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "StartViewController.h"
#import "UIView+Extension.h"
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "RankingViewController.h"
#import "MineViewController.h"
#import "SpecialViewController.h"


#define StartPhotoCount 4
@interface StartViewController ()<UIScrollViewDelegate>
@property (nonatomic,retain) UIPageControl *pageControl;
@property (nonatomic, retain) UIScrollView *scrollView;
@end

@implementation StartViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
#pragma mark - 创建scrollView显示图片
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
#pragma mark - 添加图片到scrollView
    CGFloat scrollW = self.scrollView.width;
    CGFloat scrollH = self.scrollView.height;
    for (int i = 0; i < StartPhotoCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        
        // 显示图片
        NSString *name = [NSString stringWithFormat:@"1%d.jpg", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        if (i == StartPhotoCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    // 设置scroll属性
    self.scrollView.contentSize = CGSizeMake(StartPhotoCount * self.scrollView.width, 0);
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    
#pragma mark - 添加page分页
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.numberOfPages = StartPhotoCount;
    self.pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    self.pageControl.center = CGPointMake(scrollW * 0.5, scrollH - 50);
    [self.view addSubview:self.pageControl];
    
}

#pragma mark - 实现代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = self.scrollView.contentOffset.x / self.scrollView.width;
    self.pageControl.currentPage = (int)(page + 0.5);
}

#pragma marak - 创建"立即体验"按钮
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    
    // 立即体验
    UIButton *startBtn = [[UIButton alloc] init];
    //    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    //    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = CGSizeMake(100, 50);
    startBtn.center = CGPointMake(imageView.width * 0.5, imageView.height * 0.75);
    [startBtn setTitle:@"立即体验" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

- (void)startClick
{
#pragma mark - 创建tabBar
    UIWindow *window = [[UIWindow alloc] init];
    window.frame = [[UIScreen mainScreen] bounds];
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    tabBar.tabBar.translucent = NO;
    
#pragma mark - 设置子控制器
    HomeViewController *home = [[HomeViewController alloc] init];
    UINavigationController *homeVC = [[UINavigationController alloc] initWithRootViewController:home];
    homeVC.navigationBar.translucent = NO;
    home.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"图库" image:[UIImage imageNamed:@"iconfont-tupian"] tag:1001];
    
    RankingViewController *rank = [[RankingViewController alloc]init];
    UINavigationController *rankVC = [[UINavigationController alloc] initWithRootViewController:rank];
    rankVC.navigationBar.translucent = NO;
    rank.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"排行榜" image:[UIImage imageNamed:@"iconfont-paixingbang"] tag:1002];
    
    SpecialViewController *special = [[SpecialViewController alloc] init];
    UINavigationController *specialVC = [[UINavigationController alloc] initWithRootViewController:special];
    specialVC.navigationBar.translucent = NO;
    special.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"专题" image:[UIImage imageNamed:@"iconfont-zhuanti"] tag:1003];
    
    MineViewController *mine = [[MineViewController alloc] init];
    UINavigationController *mineVC = [[UINavigationController alloc] initWithRootViewController:mine];
    mineVC.navigationBar.translucent = NO;
    mine.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"iconfont-01"] tag:1004];
    
    tabBar.viewControllers = @[homeVC, rankVC, specialVC, mineVC];
    
    window.rootViewController = tabBar;
    
    [window makeKeyAndVisible];
    
}

@end
